//===================================================================
// 西交投.NET产品框架 版权所有
//===================================================================
using System;
using System.Threading.Tasks;
using XCI.Core;
using XCI.Sys.Filter;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Extensions;
using XCI.Windows;
using XCI.Helper;

namespace XCI.Sys.Forms
{
    /// <summary>
    /// 系统序列管理
    /// </summary>
    [FormCaption("系统序列管理")]
    public partial class FrmSeqManager : BaseForm
    {
        private readonly bool allowSelect = SysApp.Permit("sys.seq.select");
        private readonly bool allowInsert = SysApp.Permit("sys.seq.insert");
        private readonly bool allowUpdate = SysApp.Permit("sys.seq.update");
        private readonly bool allowDelete = SysApp.Permit("sys.seq.delete");
        private readonly bool allowExport = SysApp.Permit("sys.seq.export");

        public FrmSeqManager()
        {
            InitializeComponent();
            gridControl.ModelType = typeof(SysSeq);
        }

        /// <summary>
        /// 加载首页数据
        /// </summary>
        private Task LoadFirstPageAsync()
        {
            return Mutex.EndAsync(() => gridControl.LoadFirstPageAsync());
        }

        /// <summary>
        /// 显示编辑窗口
        /// </summary>
        /// <param name="args">编辑参数</param>
        private void ShowEditForm(EditFormArgs args)
        {
            var editForm = new FrmSeqEdit(args);
            editForm.ShowDialogOk(async () => await gridControl.LoadAsync());
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        private async void FrmSeqManager_Load(object sender, EventArgs e)
        {
            await LoadFirstPageAsync();
        }

        /// <summary>
        /// 新建按钮事件
        /// </summary>
        private void BtnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowEditForm(EditFormArgs.Create());
        }

        /// <summary>
        /// 修改按钮事件
        /// </summary>
        private void BtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowEditForm(EditFormArgs.Edit(gridControl.SelectedId));
        }

        /// <summary>
        /// 复制按钮事件
        /// </summary>
        private async void BtnCopy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要复制选中的数据吗?") != System.Windows.Forms.DialogResult.Yes) return;
            await SysSeqService.Instance.CopyAsync(gridControl.SelectedId);
            await gridControl.LoadAsync();
        }

        /// <summary>
        /// 删除按钮事件
        /// </summary>
        private async void BtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            await gridControl.DeleteAsync();
        }

        /// <summary>
        /// 导出按钮事件
        /// </summary>
        private async void BtnExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var filter = gridControl.GetFilter();
            await App.Excel.ExportFileDialogAsync((path, fields) => SysSeqService.Instance.ExportAsync(path, filter as SysSeqFilter), "系统序列列表", this);
        }

        /// <summary>
        /// 属性按钮事件
        /// </summary>
        private void BtnProperty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridControl.InvokeRowDoubleClick();
        }

        /// <summary>
        /// 查询表格数据事件
        /// </summary>
        private void GridControl_LoadData(object sender, LoadDataEventArgs e)
        {
            e.Task = async () => await SysSeqService.Instance.SelectPageListAsync(e.Filter as SysSeqFilter);
        }

        /// <summary>
        /// 获取表格过滤对象事件
        /// </summary>
        private void GridControl_DetectFilter(object sender, DetectFilterEventArgs e)
        {
            e.Filter = App.ContainerData.GetModel(layoutGroupFilter, new SysSeqFilter());
        }

        /// <summary>
        /// 删除表格数据事件
        /// </summary>
        private void GridControl_DeleteData(object sender, DeleteDataEventArgs e)
        {
            e.Task = () => SysSeqService.Instance.DeleteAsync(gridControl.SelectedIds);
        }

        /// <summary>
        /// 表格行选中状态变化事件
        /// </summary>
        private void GridControl_RowSelectionChanged(object sender, EventArgs e)
        {
            btnNew.Enabled = allowInsert;
            btnEdit.Enabled = gridControl.HasSingleSelected && allowUpdate;
            btnCopy.Enabled = gridControl.HasSingleSelected && allowInsert;
            btnDelete.Enabled = gridControl.HasSelected && allowDelete;
            btnProperty.Enabled = gridControl.HasSingleSelected;
            btnExport.Enabled = gridControl.HasData && allowExport;
            btnSearch.Enabled = allowSelect;
        }

        /// <summary>
        /// 事件行双击事件
        /// </summary>
        private void GridControl_RowDoubleClick(object sender, EventArgs e)
        {
            new FrmSeqDetails(gridControl).ShowDialogOk();
        }

        /// <summary>
        /// 关键字回车事件
        /// </summary>
        private async void FilterName_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == R.EnterKey) await LoadFirstPageAsync();
        }

        /// <summary>
        /// 搜索按钮事件
        /// </summary>
        private async void BtnSearch_Click(object sender, EventArgs e)
        {
            await LoadFirstPageAsync();
        }

        /// <summary>
        /// 重置按钮事件
        /// </summary>
        private async void BtnReset_Click(object sender, EventArgs e)
        {
            Mutex.Begin(() => App.ContainerData.SetModel(layoutGroupFilter, SysSeqFilter.Default));
            await LoadFirstPageAsync();
        }
    }
}
